package com.net.framework.help.utils;

import java.util.Iterator;
import java.util.Map;

/**
 * Author：xiaobin.zhang
 * Email：154722566@qq.com
 * Date：2016/9/14 14:57
 * Desc：<--url拼接-->
 */
public class HttpUrlJoint {
    public static String getUrl(String url, Map<String, Object> params) {
        if (StringUtil.isBlank(url)) {
            return "";
        }
        if (params.isEmpty()) {
            return url;
        }

        boolean isNext = false;
        if (url.contains("?")) {
            isNext = true;
        }
        // 添加url参数
        if (params != null) {
            Iterator<String> it = params.keySet().iterator();
            StringBuffer sb = null;
            while (it.hasNext()) {
                String key = it.next();
                String value = params.get(key).toString();
                if (sb == null && !isNext) {
                    sb = new StringBuffer();
                    sb.append("?");
                } else {
                    if (sb == null) {
                        sb = new StringBuffer();
                    }
                    sb.append("&");
                }
                sb.append(key);
                sb.append("=");
                sb.append(value);
            }
            url += sb.toString();
        }
        return url;
    }
}
